package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.domain.Cart;
import csu.web.mypetstore.domain.CartItem;
import csu.web.mypetstore.domain.Product;
import csu.web.mypetstore.persistence.CartDao;
import csu.web.mypetstore.persistence.impl.CartDaoImpl;
import csu.web.mypetstore.service.AccountService;
import csu.web.mypetstore.service.CatalogService;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.util.List;

public class SignOnServlet extends HttpServlet {

    private static final String SIGN_ON_FROM = "/WEB-INF/jsp/account/signon.jsp";
    private String username;
    private String password;

    private String signOnMsg;


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession httpSession = req.getSession();

        this.username = req.getParameter("username");
        this.password = req.getParameter("password");
        String checkcode = req.getParameter("checkCode");
        String code = (String) req.getSession().getAttribute("randCheckCode");

        //校验用户输入的正确性
        if(!validate()){
            req.setAttribute("signOnMsg",this.signOnMsg);
            req.getRequestDispatcher(SIGN_ON_FROM).forward(req,resp);
        }else {
            if (checkcode.equals("") || checkcode == null) {
                this.signOnMsg = "验证码不能为空";
                req.setAttribute("signOnMsg", this.signOnMsg);
                req.getRequestDispatcher(SIGN_ON_FROM).forward(req, resp);
                return;
            } else {
                if (!checkcode.equalsIgnoreCase(code)) {
                    this.signOnMsg = "验证码错误";
                    req.setAttribute("signOnMsg", this.signOnMsg);
                    req.getRequestDispatcher(SIGN_ON_FROM).forward(req, resp);
                    return;
                } else {
                    AccountService accountService = new AccountService();
                    Account loginAccount = accountService.getAccount(username, password);

                    if (loginAccount == null) {  //账户验证
                        this.signOnMsg = "用户名或密码错误";
                        req.setAttribute("signOnMsg", this.signOnMsg);
                        req.getRequestDispatcher(SIGN_ON_FROM).forward(req, resp);
                    } else {
                        loginAccount.setPassword(null);
                        HttpSession session = req.getSession();
                        session.setAttribute("account", loginAccount);

                        if (loginAccount.isListOption()) {
                            CatalogService catalogService = new CatalogService();
                            List<Product> myList = catalogService.getProductListByCategory(loginAccount.getFavouriteCategoryId());
                            session.setAttribute("myList", myList);
                        }
                    }
                    // 加载购物车信息
                    CartDao cartDAO = new CartDaoImpl();
                    Cart cart = new Cart();
                    List<CartItem> cartItemList = cartDAO.getCartList(loginAccount.getUsername());

                    if (cartItemList != null) {
                        for (CartItem cartItem : cartItemList) {
                            cart.addItem(cartItem.getItem(), true);
                            cart.setQuantityByItemId(cartItem.getItem().getItemId(), cartItem.getQuantity());
                        }
                        cart.setTotal(cart.getSubTotal());
                    } else {
                        cart = null;
                    }

//                //日志记录
//                LogService logService = new LogService();
//                List<Log> logList = logService.getLog(loginAccount);
//                logService.updateLog(logList, "Log in to the pet store.", loginAccount);
//
//                httpSession.setAttribute("logList", logList);



                    httpSession.removeAttribute("signOnMsg");
                    httpSession.setAttribute("cart", cart);
                    httpSession.setAttribute("account", loginAccount);

                }
            }
        }


        resp.sendRedirect("mainFrom");

    }

    private boolean validate(){ //校验
        if(this.username == null || this.username.equals("")){
            this.signOnMsg = "用户名不能为空";
            return false;
        }


        if(this.password == null || this.password.equals("")){
            this.signOnMsg = "密码不能为空";
            return false;
        }
        return true;
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
